package User;
import book.Library;
import constant.Constant;
import util.PermissionException;
public class ProxyUser {
   private  Library library=Library.getLibrary();

    private User realuser;//被代理的真实用户；

    public ProxyUser(User user) {
        this.realuser = user;//对被代理的对象进⾏赋值
    }
    public User getRealuser(){
        return realuser;
    }
    private void checkRealUserWhetherAdminUser(String msg){
        if(!(realuser instanceof AdminUser)){
            throw new PermissionException(msg);
        }
    }
    public void addBook() {
//查看代理的对象是是不是管理员
        checkRealUserWhetherAdminUser("普通⽤⼾没有权限上架图书");
        ((AdminUser) realuser).addBook();
    }
    //更新书籍操作
    public void updateBook() {
        checkRealUserWhetherAdminUser("普通⽤⼾没有权限更新图书");
        ((AdminUser) realuser).updateBook();
    }
    //移除图书
    public void removeBook() {
        checkRealUserWhetherAdminUser("普通⽤⼾没有权限删除图书");
        ((AdminUser) realuser).removeBook();
    }
    //查看图书的借阅次数
    public void borrowCount( ) {
        checkRealUserWhetherAdminUser("普通⽤⼾没有权限查看图书的借阅次数");
        ((AdminUser) realuser).borrowCount();
    }
    //查看最受欢迎的前K本书
    public void generateBook() {
        checkRealUserWhetherAdminUser("普通⽤⼾没有权限查看最受欢迎的前k本书");
        ((AdminUser) realuser).generateBook();
    }

    //查看库存状态
    public void checkInventoryStatus() {
        checkRealUserWhetherAdminUser("普通⽤⼾没有权限查看库存状态");
        ((AdminUser) realuser).checkInventoryStatus();
    }
    //移除上架超过1年的书籍
    public void checkAndRemoveOldBooks() {
        checkRealUserWhetherAdminUser("普通⽤⼾没有权限移除上架超过⼀年的图书");
        ((AdminUser) realuser).checkAndRemoveOldBooks();
    }
    //--------------------------------普通相关⽅法------------------------------//
    private void checkRealUserWhetherNormalUser(String exceptionMessage) {
        if(!(realuser instanceof NormalUser)){
            throw new PermissionException(exceptionMessage);
        }
    }
    //借阅图书
    public void borrowBook() {
        checkRealUserWhetherNormalUser("管理员请以普通⽤⼾的⽅式借阅图书");
        ((NormalUser) realuser).borrowBook();
    }
    //归还图书
    public void returnBook() {
        checkRealUserWhetherNormalUser("管理员请以普通⽤⼾的⽅式归还图书");
        ((NormalUser) realuser).returnBook();
    }
    //查看个⼈借阅情况
    public void viewBorrowHistory() {
        checkRealUserWhetherNormalUser("管理员请以普通⽤⼾的⽅式查看个⼈当前借阅情况");
        ((NormalUser) realuser).viewBorrowBooks();
    }

    public void handleOperation(int choice) {
        if (realuser instanceof AdminUser) {
// 管理员操作
            switch (choice) {
                case Constant.SEARCH_BOOK:
                    library.searchBook();
                    break;
                case Constant.DISPLAY_BOOK:
                    library.displayBook();
                    break;
                case Constant.EXIT:
                    library.exit();
                    break;
                case Constant.ADD_BOOK:
                    addBook();
                    break;
                case Constant.UPDATE_BOOK:
                    updateBook();
                    break;
                case Constant.REMOVE_BOOK:
                    removeBook();
                    break;
                case Constant.BORROWED_BOOK_COUNT:
                    borrowCount();
                    break;
                case Constant.GENERATE_BOOK:
                    generateBook();
                    break;
                case Constant.CHECK_INVENTORY_STATUS:
                    checkInventoryStatus();
                    break;
                case Constant.CHECK_AND_REMOVE_OLD_BOOK:
                    checkAndRemoveOldBooks();
                    break;
                default:
                    System.out.println("⽆效的操作。");
            }
        }else if (realuser instanceof NormalUser) {
// 普通⽤⼾操作
            switch (choice) {
                case Constant.SEARCH_BOOK:
                    library.searchBook();
                    break;
                case Constant.DISPLAY_BOOK:
                    library.displayBook();
                    break;
                case Constant.EXIT:
                    library.exit();
                case Constant.BORROWED_BOOK:
                    borrowBook();
                    break;
                case Constant.RETURN_BOOK:
                    returnBook();
                    break;
                case Constant.VIEW_BORROW_HISTORY_BOOK:
                    viewBorrowHistory();
                    break;
            }

  //  public void borrowBook(String bookname) {
  //      System.out.println("检查用户权限");
  //      if(realuser instanceof NormalUser){
  //          ((NormalUser) realuser).borrowBook();
  //      } else if (realuser instanceof AdminUser) {
  //          System.out.println("您没有权限借阅书籍，请以普通⽤⼾的⽅式借阅书籍");
     //   }
    //}
   // public void addBook(){
       // System.out.println("检查用书本状态");
     //   if(realuser instanceof AdminUser){
       //     ((AdminUser) realuser).addBook();
       // } else if (realuser instanceof NormalUser) {
         //   System.out.println("您没有权限上架书籍，请以管理员⽤⼾的⽅式上架书籍");

        }

    }

}
